package com.itsci.assignment1;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class Add extends Activity {
	private EditText name;
	private EditText address;
	private EditText phone;
	private EditText birthday;
	private EditText height;
	private EditText age;
	private EditText weight;
	private EditText appear;
	private EditText be;
	private Button btn;
	private Button clear;
	private String path = "/mnt/sdcard/TextFile/Patient.txt";
	private Button take;
	private ImageView img;
	private Uri imageUri;
	private Button browse;
	private String photo;
	// private int id ;
	// private String text[];
	String line;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addpatient);
		name = (EditText) findViewById(R.id.editView_add_Pname);
		address = (EditText) findViewById(R.id.editText_add_PAddress);
		phone = (EditText) findViewById(R.id.editText_add_PPhone);
		birthday = (EditText) findViewById(R.id.editView_add_PBirthday);
		height = (EditText) findViewById(R.id.editView_add_PHeight);
		age = (EditText) findViewById(R.id.editView_add_PAge);
		weight = (EditText) findViewById(R.id.editView_add_PWeight);
		appear = (EditText) findViewById(R.id.editView_add_PAppear);
		be = (EditText) findViewById(R.id.editView_add_PBe);
		take = (Button) findViewById(R.id.button_addP_TakeAPhoto);
		img = (ImageView) findViewById(R.id.imageView_addP);

		take.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
				File photo = new File(
						Environment.getExternalStorageDirectory(), "Pic.jpg");
				intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
				imageUri = Uri.fromFile(photo);
				startActivityForResult(intent, 111);

			}
		});
		browse = (Button) findViewById(R.id.button_aaP_Browse);
		browse.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
				photoPickerIntent.setType("image/*");
				startActivityForResult(photoPickerIntent, 222);

			}
		});

		btn = (Button) findViewById(R.id.button_addP);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {

					File file = new File(path);
					if (!file.exists()) {
						try {
							file.createNewFile();
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
					FileWriter writer = new FileWriter(file, true);

					writer.write(name.getText() + "," + birthday.getTextSize()
							+ "," + age.getText() + "," + height.getText()
							+ "," + weight.getText() + "," + address.getText()
							+ "," + phone.getText() + "," + appear.getText()
							+ "," + be.getText() +","+photo+"\n");
					writer.close();
					Toast.makeText(Add.this, "File Save!", Toast.LENGTH_LONG)
							.show();
				} catch (Exception e) {
					e.printStackTrace();
					Toast.makeText(Add.this, "Failed!" + e.getMessage(),
							Toast.LENGTH_LONG).show();

				}
				Intent gopage = new Intent(Add.this, ShowPatient.class);
				startActivity(gopage);

			}

		});
		clear = (Button) findViewById(R.id.button_ClearAdd);
		clear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				name.setText("");
				address.setText("");
				phone.setText("");
				birthday.setText("");
				height.setText("");
				age.setText("");
				weight.setText("");
				appear.setText("");
				be.setText("");
			}
		});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 111: {
			if (resultCode == Activity.RESULT_OK) {
				Uri selectedImage = imageUri;
				getContentResolver().notifyChange(selectedImage, null);
				ContentResolver cr = getContentResolver();
				Bitmap bitmap;
				try {
					bitmap = android.provider.MediaStore.Images.Media
							.getBitmap(cr, selectedImage);

					img.setImageBitmap(bitmap);
					Toast.makeText(this, selectedImage.toString(),
							Toast.LENGTH_LONG).show();
					photo = selectedImage.toString();
				} catch (Exception e) {
					Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
							.show();
					Log.e("Camera", e.toString());
				}
			}
			break;
		}
		case 222: {
			if (resultCode == RESULT_OK) {
				Uri photoUri = data.getData();
				if (photoUri != null) {
					try {
						Bitmap currentImage = MediaStore.Images.Media
								.getBitmap(this.getContentResolver(), photoUri);
						img.setImageBitmap(currentImage);
						photo = photoUri.toString();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
			break;
		}
	}

}
